package com.tanhua.manage.controller;

import com.tanhua.manage.service.MessageService;
import com.tanhua.manage.vo.ManageMessages;
import com.tanhua.manage.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZJWzxy  陈亮
 */
@RestController
@RequestMapping("management/manage/messages")
@Slf4j
public class MessageController {

    @Autowired
    private MessageService messageService;

    /*
     * 消息置顶
     * */
    @RequestMapping("{id}/top")
    public ResponseEntity<String> topMessage(@PathVariable("id") String id) {
        //接收publishVo的动态id 动态为message
        try {
            String message = this.messageService.topMessage(id);
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息管理--消息翻页
     *
     * @param pagesize  页大小
     * @param page      页码
     * @param sortProp  排序字段
     * @param sortOrder ascending 升序 descending 降序
     * @param userId    用户id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param state     审核状态
     * @param uid  用户管理中的动态分页
     * @return 分页结果
     */
    @GetMapping
    public ResponseEntity<PageResult> queryMessageList(@RequestParam(value = "pagesize", defaultValue = "10", required = false) Integer pagesize,
                                                       @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                                                       @RequestParam("sortProp") String sortProp,
                                                       @RequestParam("sortOrder") String sortOrder,
                                                       @RequestParam(value = "id", required = false) Integer userId,
                                                       @RequestParam(value = "sd", required = false) String startDate,
                                                       @RequestParam(value = "ed", required = false) String endDate,
                                                       @RequestParam(value = "state", defaultValue = "3") String state,
                                                       @RequestParam(value = "uid",required = false) Integer uid) {
        try {
            //调用MessageService中的查询方法得到所有的动态
            PageResult pageResult = this.messageService.queryMessageList(page, pagesize, sortProp, sortOrder, userId, startDate, endDate, state,uid);
            if (null != pageResult) {
                //查询成功
                String msg = "查询所有动态成功";
                log.info(msg);
                return ResponseEntity.ok(pageResult);
            }
        } catch (Exception e) {
            //查询失败
            String msg = "查询所有动态失败";
            log.info(msg, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }

    /*
     * 取消置顶
     * */
    @RequestMapping("{id}/untop")
    public ResponseEntity<String> unTopMessage(@PathVariable("id") String id) {
        try {
            String message = this.messageService.unTopMessage(id);
            return ResponseEntity.ok(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();

    }


    /**
     * 消息详情
     * 发布表 评论表
     * 新建一张表，把发布评论二表整合，返回数据
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ResponseEntity<ManageMessages> messages(@PathVariable("id") String id) {
        try {
            ManageMessages pageRequest = messageService.messages(id);
            return ResponseEntity.ok(pageRequest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息通过
     *
     * @param ids 评论id列表
     * @return null
     */
    @PostMapping("pass")
    public ResponseEntity<String> passMessage(@RequestBody String[] ids) {
        try {
            //调用MessageService中的方法
            boolean bool = this.messageService.passMessage(ids);
            if (bool) {
                //消息通过成功
                String msg = "消息通过成功";
                log.info(msg);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //消息通过失败
            String msg = "消息通过失败";
            log.info(msg, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 消息拒绝
     *
     * @param ids 评论id列表
     * @return null
     */
    @PostMapping("reject")
    public ResponseEntity<String> rejectMessage(@RequestBody String[] ids) {
        try {
            //调用MessageService中的方法
            boolean bool = this.messageService.rejectMessage(ids);
            if (bool) {
                //消息拒绝成功
                String msg = "消息拒绝成功";
                log.info(msg);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //消息通过失败
            String msg = "消息拒绝失败";
            log.info(msg, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


    /**
     * 消息撤销
     *
     * @param ids 评论id列表
     * @return null
     */
    @PostMapping("revocation")
    public ResponseEntity<String> revocationMessage(@RequestBody String[] ids) {
        try {
            //调用MessageService中的方法
            boolean bool = this.messageService.revocationMessage(ids);
            if (bool) {
                //消息撤销成功
                String msg = "消息撤销成功";
                log.info(msg);
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            //消息撤销失败
            String msg = "消息撤销失败";
            log.info(msg, e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

}